package com.peas.platform.module.icims.budget.archives.domain.impl;

import cn.hutool.core.util.StrUtil;
import com.peas.platform.framework.mybatisplus.domain.BaseDomainImpl;
import com.peas.platform.framework.mybatisplus.repository.BaseRepository;
import com.peas.platform.module.icims.base.sys.service.SequenceService;
import com.peas.platform.module.icims.budget.archives.adapter.repository.BudgetItemRangeRepository;
import com.peas.platform.module.icims.budget.archives.adapter.repository.po.BudgetItemRangePo;
import com.peas.platform.module.icims.budget.archives.domain.BudgetItemRangeDomain;
import com.peas.platform.module.icims.budget.archives.dto.BudgetItemRangeDto;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Component
public class BudgetItemRangeDomainImpl extends BaseDomainImpl<BudgetItemRangeDto, BudgetItemRangePo> implements BudgetItemRangeDomain {

    @Resource
    private BudgetItemRangeRepository budgetItemRangeRepository;
    @Resource
    private SequenceService sequenceService;

    @Override
    public BaseRepository<BudgetItemRangePo> getMapper() {
        return budgetItemRangeRepository;
    }

    @Override
    public Class<? extends BaseRepository<BudgetItemRangePo>> getRepositoryClass() {
        return BudgetItemRangeRepository.class;
    }

    @Transactional
    @Override
    public int save(BudgetItemRangeDto dto) {
        if (StrUtil.isBlank(dto.getItemRangeCode())) {
            String code = sequenceService.genSequence("budget", "BudgetItemRangeDto", "BIR", 4, 1);
            dto.setItemRangeCode(code);
        }
        int result = super.save(dto);
        this.checkRepetition("itemRangeCode", dto.getItemRangeCode());
        return result;
    }

    @Transactional
    @Override
    public int updateById(BudgetItemRangeDto dto) {
        int result = super.updateById(dto);
        this.checkRepetition("itemRangeCode", dto.getItemRangeCode());
        return result;
    }

}
